package org.truenewx.tnxjeex.unit.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.truenewx.tnxjee.core.enums.EnumDictResolver;
import org.truenewx.tnxjee.core.util.ClassUtil;
import org.truenewx.tnxjee.core.util.JacksonUtil;
import org.truenewx.tnxjeex.unit.model.MeasureUnit;
import org.truenewx.tnxjeex.unit.support.MeasureUnits;

@Configuration
@SuppressWarnings("unchecked")
public class UnitConfiguration {

    static {
        JacksonUtil.NON_CONCRETE_DECIDERS.add(clazz -> {
            if (MeasureUnit.class.isAssignableFrom(clazz)) {
                return false;
            }
            return null;
        });

        ClassUtil.loopClass(MeasureUnit.class.getPackageName(), clazz -> {
            if (clazz != MeasureUnit.class && MeasureUnit.class.isAssignableFrom(clazz)) {
                MeasureUnits.register((Class<? extends MeasureUnit<?>>) clazz);
            }
        });
    }

    @Autowired
    public void setEnumDictResolver(EnumDictResolver enumDictResolver) {
        enumDictResolver.addBasePackageName(MeasureUnit.class.getPackageName());
    }

}
